import FishBase from "./FishBase";
import DialogBase from "../../script/engine/uicomponent/DialogBase";
import { FishConfig } from "../../script/game/config/FishConfig";
import { FishInfo } from "../../script/game/config/FishInfo";
import FishManager from "../../script/game/manager/FishManager";
import PrefabLoader from "../../script/engine/utils/PrefabLoader";
import { GameConfig } from "../../script/game/config/GameConfig";
import BundleManager from "../../../base/Script/core/BundleManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class FishWiki extends DialogBase {

    @property({ type: cc.ScrollView })
    private scrollView: cc.ScrollView = null;

    @property({ type: cc.Prefab })
    private wikiItemPrefab: cc.Prefab = null;

    onLoadMe() {
        this.init();
    }

    private init() {
        this.scrollView.content.removeAllChildren();
        this.initOne(0)
    }

    private initOne(index: number) {
        if (index < FishConfig.config.length) {
            let itemNode: cc.Node = cc.instantiate(this.wikiItemPrefab)

            this.scrollView.content.addChild(itemNode);
            let fishInfo:FishInfo = FishConfig.config[index];
            let txtName: cc.Label = itemNode.getChildByName("txtName").getComponent(cc.Label);
            txtName.string = fishInfo.name;
            let txtLife:cc.Label = itemNode.getChildByName("txtLife").getComponent(cc.Label);
            txtLife.string = "life:"+fishInfo.blood + ""
            let view: cc.Node = itemNode.getChildByName("view");
            view.removeAllChildren();

            let fish: FishBase = FishManager.instance.createFishByType(fishInfo.fishType);
            view.addChild(fish.node);
            fish.node.setPosition(0,0)
            fish.node.setScale(fishInfo.wikiScale);
            fish.node.getComponent(cc.Animation).play();
            this.scheduleOnce(() => {
                this.initOne(index + 1)
            }, 0.05)
        }
    }


    public static show(parentNode: cc.Node = null) {

        PrefabLoader.loadPrefab("prefab/"+"game/dialog/FishWiki", (loadedResource) => {
            if (!parentNode) {
                parentNode = cc.Canvas.instance.node;
            }
            let node: cc.Node = cc.instantiate(loadedResource);
            parentNode.addChild(node);
            node.setPosition(0, 0)
        });
    }
}
